<?php

/**
 * PaymentPanelController is the default controller for this application
 */
class PaymentPanelController extends BrowseControllerBase {
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		$this->requireUserLogin ();
		
	/*
		//Check if User is logged in
		$authentication = new Authentication();
		if ($authentication->getCurrentUserId() == null) {
			// Go on to login page
			$url = Zend_Registry::get('config')->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect($url);
			return -1;
		}
		*/
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/list_payment.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/**
	 * Edit Payment Settings Values
	 */
	public function editAction() {
		// Get id of page
		$type = $this->getPost ( 'type' );
		if (empty ( $type )) {
			$type = $this->getQuery ( 'type' );
		}
		
		// If the id of page does not exist, go on to view list page
		if (empty ( $type )) {
			$this->indexAction ();
			return false;
		}
		$authentication = new Authentication ();
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		$userId = $authentication->getCurrentUserId ();
		
		// Get Paypay
		$paypalUsernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_USERNAME, $userId );
		$paypalPasswordSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_PASSWORD, $userId );
		$paypalSignatureSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_SIGNATURE, $userId );
		$paypalTestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_TESTMODE, $userId );
		$paypalEnableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_ENABLE, $userId );
		
		// Get 2Checkout Settings
		$checkoutUsernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_USERNAME, $userId );
		$checkoutPasswordSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_PASSWORD, $userId );
		$checkoutTestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_TESTMODE, $userId );
		$checkoutEnableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_ENABLE, $userId );
		$checkoutSidSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_SID, $userId );
		
		//google checkout
		$googlecheckout_MerchantIdSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_GOOGLECHECKOUT_MERCHANTID, $userId );
		$googlecheckout_MerchantKeySettingsEntity = $this->settingsDataService->getItem ( SETTINGS_GOOGLECHECKOUT_MERCHANTKEY, $userId );
		$googlecheckout_MerchantModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_GOOGLECHECKOUT_MODE, $userId );
		$googlecheckout_MerchantEnableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_GOOGLECHECKOUT_ENABLE, $userId );
		
		//worldpay
		$worldpayIdSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_WORLDPAY_INSTALLATIONID, $userId );
		$worldpayPassSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_WORLDPAY_PASSWORD, $userId );
		$worldpaytestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_WORLDPAY_MODE, $userId );
		$worldpayIdEnableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_WORLDPAY_ENABLE, $userId );
		
		//eway
		$ewaycustomeridSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_EWAY_CUSTOMERID, $userId );
		$ewayusernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_EWAY_USERNAME, $userId );
		$ewayreturnurlSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_EWAY_RETURN_URL, $userId );
		$ewaycancelurlSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_EWAY_CANCEL_URL, $userId );
		$ewayenableSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_EWAY_ENABLE, $userId );
		$ewaytestaccountSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_EWAY_TESTACCOUNT, $userId );
		
		$this->smarty->assign ( 'type', $type );
		
		$isError = false;
		$error = "";
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			
			if ($type == PAYMENT_PAYPAL) {
				// Get API Username
				$paypal_username = $this->getPost ( 'username', "" );
				$paypal_username = trim ( $paypal_username );
				
				// Get API Password
				$paypal_password = $this->getPost ( 'password', "" );
				$paypal_password = trim ( $paypal_password );
				
				// Get API Signature
				$paypal_signature = $this->getPost ( 'signature', "" );
				$paypal_signature = trim ( $paypal_signature );
				
				$paypal_testmode = $this->getPost ( 'testmode', "0" );
				
				if ($paypal_testmode == "on") {
					$paypal_testmode = "1";
				}
				
				$testMode = ($paypal_testmode == "1") ? true : false;
				
				$paypal_enable = $this->getPost ( 'paypal_enable', "0" );
				if ($paypal_enable == "on") {
					$paypal_enable = "1";
				}
				
				//Validate Paypal settings
				$message = "";
				$isValid = $this->settingsDataService->validatePaypalSettings ( $paypal_username, $paypal_password, $paypal_signature, $message, $testMode );
				
				if (! $isValid) {
					$isError = true;
					$error = $message;
					
					//Disable this Payment Gateway
					$paypalEnableSettingsEntity->value = "0";
					$this->settingsDataService->update ( $paypalEnableSettingsEntity );
				} else {
					
					$paypalUsernameSettingsEntity->value = $paypal_username;
					$this->settingsDataService->update ( $paypalUsernameSettingsEntity );
					$paypalPasswordSettingsEntity->value = $paypal_password;
					$this->settingsDataService->update ( $paypalPasswordSettingsEntity );
					$paypalSignatureSettingsEntity->value = $paypal_signature;
					$this->settingsDataService->update ( $paypalSignatureSettingsEntity );
					$paypalTestModeSettingsEntity->value = $paypal_testmode;
					$this->settingsDataService->update ( $paypalTestModeSettingsEntity );
					$paypalEnableSettingsEntity->value = $paypal_enable;
					$this->settingsDataService->update ( $paypalEnableSettingsEntity );
					
					$isError = true;
					$error = "Your settings have been saved successfully";
				}
			} else if ($type == PAYMENT_2CHECKOUT) {
				
				// Get API Username
				$checkout_username = $this->getPost ( 'username', "" );
				$checkout_username = trim ( $checkout_username );
				
				// Get API Password
				$checkout_password = $this->getPost ( 'password', "" );
				$checkout_password = trim ( $checkout_password );
				
				$checkout_testmode = $this->getPost ( 'testmode', "0" );
				
				if ($checkout_testmode == "on") {
					$checkout_testmode = "1";
				}
				
				$testMode = ($checkout_testmode == "1") ? true : false;
				
				$checkout_enable = $this->getPost ( 'checkout_enable', "0" );
				if ($checkout_enable == "on") {
					$checkout_enable = "1";
				}
				$enable = ($checkout_enable == "1") ? true : false;
				
				$message = "";
				$sid = "";
				//Validate Paypal settings
				$isValid = $this->settingsDataService->validate2CheckoutSettings ( $checkout_username, $checkout_password, $message, $testMode, $enable, $sid );
				
				if (! $isValid) {
					$isError = true;
					$error = $message;
					
					//Disable this Payment Gateway
					$checkoutEnableSettingsEntity->value = "0";
					$this->settingsDataService->update ( $checkoutEnableSettingsEntity );
				} else {
					
					$checkoutSidSettingsEntity->value = $sid;
					$this->settingsDataService->update ( $checkoutSidSettingsEntity );
					$checkoutUsernameSettingsEntity->value = $checkout_username;
					$this->settingsDataService->update ( $checkoutUsernameSettingsEntity );
					$checkoutPasswordSettingsEntity->value = $checkout_password;
					$this->settingsDataService->update ( $checkoutPasswordSettingsEntity );
					$checkoutTestModeSettingsEntity->value = $checkout_testmode;
					$this->settingsDataService->update ( $checkoutTestModeSettingsEntity );
					$checkoutEnableSettingsEntity->value = $checkout_enable;
					$this->settingsDataService->update ( $checkoutEnableSettingsEntity );
					
					$isError = true;
					$error = "Your settings have been saved successfully";
				}
			} else if ($type == PAYMENT_GOOGLECHECKOUT) {
				$googlecheckout_merchantId = $this->getPost ( 'merchantid' );
				$googlecheckout_merchantId = trim ( $googlecheckout_merchantId );
				
				$googlecheckout_merchantKey = $this->getPost ( 'merchantkey', "" );
				$googlecheckout_merchantKey = trim ( $googlecheckout_merchantKey );
				
				$googlecheckout_enable = $this->getPost ( 'googlecheckout_enable', "0" );
				if ($googlecheckout_enable == "on") {
					$googlecheckout_enable = "1";
				}
				$googlecheckout_mode = $this->getPost ( 'googlecheckout_mode', "0" );
				if ($googlecheckout_mode == "on") {
					$googlecheckout_mode = "1";
				}
				
				$googlecheckout_MerchantIdSettingsEntity->value = $googlecheckout_merchantId;
				$this->settingsDataService->update ( $googlecheckout_MerchantIdSettingsEntity );
				$googlecheckout_MerchantKeySettingsEntity->value = $googlecheckout_merchantKey;
				$this->settingsDataService->update ( $googlecheckout_MerchantKeySettingsEntity );
				$googlecheckout_MerchantModeSettingsEntity->value = $googlecheckout_mode;
				$this->settingsDataService->update ( $googlecheckout_MerchantModeSettingsEntity );
				$googlecheckout_MerchantEnableSettingsEntity->value = $googlecheckout_enable;
				$this->settingsDataService->update ( $googlecheckout_MerchantEnableSettingsEntity );
			} else if ($type == PAYMENT_WORLDPAY) {
				
				$worlpayid = $this->getPost ( 'worldpayid' );
				$worlpayid = trim ( $worlpayid );
				
				$worldpaypass = $this->getPost ( 'password', "" );
				$worldpaypass = trim ( $worldpaypass );
				
				$worldpay_enable = $this->getPost ( 'worldpay_enable', "0" );
				if ($worldpay_enable == "on") {
					$worldpay_enable = "1";
				}
				$testmode = $this->getPost ( 'testmode', "0" );
				if ($testmode == "on") {
					$testmode = "1";
				}
				// Need Validation here
				

				$worldpayIdSettingsEntity->value = $worlpayid;
				$this->settingsDataService->update ( $worldpayIdSettingsEntity );
				$worldpayPassSettingsEntity->value = $worldpaypass;
				$this->settingsDataService->update ( $worldpayPassSettingsEntity );
				$worldpaytestModeSettingsEntity->value = $testmode;
				$this->settingsDataService->update ( $worldpaytestModeSettingsEntity );
				$worldpayIdEnableSettingsEntity->value = $worldpay_enable;
				$this->settingsDataService->update ( $worldpayIdEnableSettingsEntity );
			
			} else if ($type == PAYMENT_EWAY) {
				
				$eway_customerid = $this->getPost ( 'customerid', "" );
				$eway_customerid = trim ( $eway_customerid );
				
				$eway_username = $this->getPost ( 'username', "" );
				$eway_username = trim ( $eway_username );
				
				$eway_return_url = $this->getPost ( 'return_url', "" );
				$eway_return_url = trim ( $eway_return_url );
				
				$eway_cancel_url = $this->getPost ( 'cancel_url', "" );
				$eway_cancel_url = trim ( $eway_cancel_url );
				
				$eway_enable = $this->getPost ( 'enable', "0" );
				if ($eway_enable == "on") {
					$eway_enable = "1";
				}
				$eway_testaccount = $this->getPost ( 'testaccount', "0" );
				if ($eway_testaccount == "on") {
					$eway_testaccount = "1";
				}
				// Need Validation here
				

				$ewaycustomeridSettingsEntity->value = $eway_customerid;
				$this->settingsDataService->update ( $ewaycustomeridSettingsEntity );
				
				$ewayusernameSettingsEntity->value = $eway_username;
				$this->settingsDataService->update ( $ewayusernameSettingsEntity );
				
				$ewayreturnurlSettingsEntity->value = $eway_return_url;
				$this->settingsDataService->update ( $ewayreturnurlSettingsEntity );
				
				$ewaycancelurlSettingsEntity->value = $eway_cancel_url;
				$this->settingsDataService->update ( $ewaycancelurlSettingsEntity );
				
				$ewaytestaccountSettingsEntity->value = $eway_testaccount;
				$this->settingsDataService->update ( $ewaytestaccountSettingsEntity );
				
				$ewayenableSettingsEntity->value = $eway_enable;
				$this->settingsDataService->update ( $ewayenableSettingsEntity );
			}
		
		}
		
		switch ($type) {
			case PAYMENT_PAYPAL :
				$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/edit_paypal.tpl' );
				$this->smarty->assign ( 'username', $paypalUsernameSettingsEntity->value );
				$this->smarty->assign ( 'password', $paypalPasswordSettingsEntity->value );
				$this->smarty->assign ( 'signature', $paypalSignatureSettingsEntity->value );
				$this->smarty->assign ( 'testmode', $paypalTestModeSettingsEntity->value );
				$this->smarty->assign ( 'paypal_enable', $paypalEnableSettingsEntity->value );
				
				break;
			case PAYMENT_2CHECKOUT :
				$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/edit_2checkout.tpl' );
				$this->smarty->assign ( 'username', $checkoutUsernameSettingsEntity->value );
				$this->smarty->assign ( 'password', $checkoutPasswordSettingsEntity->value );
				$this->smarty->assign ( 'testmode', $checkoutTestModeSettingsEntity->value );
				$this->smarty->assign ( 'checkout_enable', $checkoutEnableSettingsEntity->value );
				
				break;
			case PAYMENT_GOOGLECHECKOUT :
				$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/edit_googlecheckout.tpl' );
				$this->smarty->assign ( 'googlecheckout_mode', $googlecheckout_MerchantModeSettingsEntity->value );
				$this->smarty->assign ( 'googlecheckout_merchantid', $googlecheckout_MerchantIdSettingsEntity->value );
				$this->smarty->assign ( 'googlecheckout_merchantkey', $googlecheckout_MerchantKeySettingsEntity->value );
				$this->smarty->assign ( 'googlecheckout_enable', $googlecheckout_MerchantEnableSettingsEntity->value );
				break;
			case PAYMENT_WORLDPAY :
				$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/edit_worldpay.tpl' );
				$this->smarty->assign ( 'testMode', $worldpaytestModeSettingsEntity->value );
				$this->smarty->assign ( 'password', $worldpayPassSettingsEntity->value );
				$this->smarty->assign ( 'worldpay_id', $worldpayIdSettingsEntity->value );
				$this->smarty->assign ( 'worldpay_enable', $worldpayIdEnableSettingsEntity->value );
				break;
			case PAYMENT_EWAY :
				$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/edit_eway.tpl' );
				$this->smarty->assign ( 'testaccount', $ewaytestaccountSettingsEntity->value );
				$this->smarty->assign ( 'customerid', $ewaycustomeridSettingsEntity->value );
				$this->smarty->assign ( 'username', $ewayusernameSettingsEntity->value );
				$this->smarty->assign ( 'return_url', $ewayreturnurlSettingsEntity->value );
				$this->smarty->assign ( 'cancel_url', $ewaycancelurlSettingsEntity->value );
				$this->smarty->assign ( 'enable', $ewayenableSettingsEntity->value );
				break;
		}
		
		// Assign Error Variables
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'error', $error );
		
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
